//------------Sonic CD Eggman Script-------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

//-------Aliases-------//
#alias 48: TYPE_EGGMAN
#alias Player.Value1:Player.AbilityTimer
#alias Player.Value3:Player.SpeedShoes
#alias Player.Value9:Player.CameraOffset
#alias Player.Value1:Player.AbilityTimer

// Function declarations
#function Eggman_Function51
#function PlayerObject_StartJump

function Eggman_Function51
	Object.YPos-=2097152
	ObjectTileGrip(0,6,176,0)
	if CheckResult==0
		Object.YPos+=1048576
		ObjectTileGrip(0,6,176,0)
		if CheckResult==0
			Object.YPos+=1048576
			ObjectTileGrip(0,6,176,0)
			if CheckResult==0
				Object.YPos+=1048576
			endif
		endif
	endif
endfunction


function PlayerObject_StartJump	
	CheckResult=Player.JumpPress
	if Player.Speed==0
		if Stage.PlayerListPos==0
			if Player.Up==1
				CheckResult=0
			endif
		endif
		if Player.Down==1
			CheckResult=0
		endif
	endif
	if CheckResult==1
		CheckResult=0
		if Player.CollisionMode==0
			TempValue6=Object.XPos
			TempValue7=Object.YPos
			Object.XPos=Player.XPos
			Object.YPos=Player.YPos
			TempValue0=Player.CollisionTop
			TempValue0-=2
			ObjectTileCollision(2,0,TempValue0,0)
			Object.XPos=TempValue6
			Object.YPos=TempValue7
		endif
		if CheckResult==0
			Player.ControlLock=0
			Player.Gravity=1
			if Stage.PlayerListPos==0
				Player.AbilityTimer=6
			else
				Player.AbilityTimer=8
			endif
			if Stage.Seconds>2
				Sin256(TempValue0,Player.Angle)
				TempValue0*=Player.JumpStrength
				TempValue0>>=8
				Player.XVelocity+=TempValue0
				Cos256(TempValue0,Player.Angle)
				TempValue0*=Player.JumpStrength
				TempValue0>>=8
				Player.YVelocity-=TempValue0
			else
				Sin256(Player.XVelocity,Player.Angle)
				Player.XVelocity*=Player.JumpStrength
				Cos256(TempValue0,Player.Angle)
				TempValue0*=Player.Speed
				Player.XVelocity+=TempValue0
				Player.XVelocity>>=8
				Sin256(Player.YVelocity,Player.Angle)
				Player.YVelocity*=Player.Speed
				Cos256(TempValue0,Player.Angle)
				TempValue0*=Player.JumpStrength
				Player.YVelocity-=TempValue0
				Player.YVelocity>>=8
			endif
			Player.Speed=Player.XVelocity
			Player.TrackScroll=1
			Player.Animation=ANI_JUMPING
			Player.Angle=0
			Player.CollisionMode=0
			Player.Timer=1
			CallFunction(PlayerObject_RollAnimSpeed)
			Player.State=PlayerObject_HandleAir
			PlaySfx(0,0)
		endif
	endif
endfunction


sub ObjectMain
	switch Object.State
	case 0
		Stage.XBoundary2=Object.iXPos
		Stage.XBoundary2-=64
		TempValue0=Stage.XBoundary2
		TempValue0-=Screen.XSize
		Stage.NewXBoundary1=TempValue0
		PlayerObjectCollision(C_TOUCH,-256,-256,0,256)
		if CheckResult==1
			CreateTempObject(TypeName[FadeMusic],0,Object.XPos,Object.YPos)
			Object.Direction=FACING_LEFT
			Object.Priority=1
			Object.State++
		endif
		break
	case 1
		if Object.Value0<120
			Object.Value0++
		else
			Object.Value0=0
			Object.Animation=1
			Object.State++
		endif
		break
	case 2
		if Object.Value0<140
			Object.XPos-=393216
			Object.Value0++
		else
			Object.Value0=0
			Object.Direction=FACING_RIGHT
			Object.State++
		endif
		break
	case 3
		if Object.Value0<160
			Object.XPos+=393216
			Object.Value0++
		else
			Object.Value0=0
			Object.Direction=FACING_LEFT
			Object.State++
		endif
		break
	case 4
		if Object.Value0<47
			Object.XPos-=393216
			Object.Value0++
		else
			Object.Value0=0
			Object.Animation=0
			Object.State++
		endif
		break
	case 5
		if Object.Value0<94
			Object.Value0++
			if Object.Value0==30
				Object[+1].State=1
				Object[+1].Animation=2
				Object[+1].Value0=0
			endif
			if Object.Value0==90
				Object.Value2=1
			endif
		else
			Object.Value0=0
			Object.Animation=1
			Object.Value5=-393216
			Object.State++
		endif
		break
	case 6
		Object.XPos-=131072
		if Object.Value0<6
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		break
	case 7
		Object.XPos-=131072
		if Object.Value0<30
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=1
			Object.State++
		endif
		break
	case 8
		Object.XPos-=131072
		if Object.Value0<10
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		break
	case 9
		Object.XPos-=131072
		if Object.Value0<30
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=1
			Object.State++
		endif
		break
	case 10
		Object.XPos-=131072
		if Object.Value0<10
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		CallFunction(Eggman_Function51)
		break
	case 11
		Object.XPos-=131072
		if Object.Value0<120
			Object.Value0++
		else
			SetMusicTrack("JP/R7DBoss.ogg",0,1)
			CreateTempObject(TypeName[FadeMusic],2,Object.XPos,Object.YPos)
			Object[-2].State=1
			Object[-1].State=3
			Object[-1].Value0=4194304
			Object[-1].YPos-=4194304
			Object.Value0=0
			Object.Direction=FACING_RIGHT
			Object.Value2=1
			Object.Value5=393216
			Object.Value1=131072
			Object.State++
		endif
		CallFunction(Eggman_Function51)
		break
	case 12
		Object.XPos+=Object.Value1
		TempValue0=Object.iXPos
		TempValue0-=16
		if Stage.XBoundary2<16352
			TempValue1=Player.XVelocity
			TempValue1>>=16
			Stage.XBoundary2+=6
			Stage.XBoundary2+=TempValue1
		endif
		if Stage.XBoundary1<TempValue0
			Stage.XBoundary1=TempValue0
		endif
		if Object.Value0<360
			Object.Value0++
		else
			Object.Value0=0
			Stage.XBoundary2=Object[-1].iXPos
			Stage.XBoundary2+=112
			Object.State++
		endif
		CallFunction(Eggman_Function51)
		break
	case 13
		TempValue0=Object.Value1
		Object.Value6++
		Object.Value6&=16383
		TempValue1=Object.Value6
		TempValue1>>=4
		Object.Value1+=TempValue1
		if Object.Value1>327680
			Object.Value1=327680
		endif
		TempValue1=Screen.XOffset
		TempValue1<<=16
		TempValue1-=Object.XPos
		if TempValue1>0
			TempValue1>>=7
			if TempValue1>262144
				TempValue1=262144
			endif
			TempValue0+=TempValue1
		endif
		if TempValue0>327680
			TempValue0=327680
		endif
		if TempValue0>196608
			TempValue0-=196608
			Object.XPos+=196608
			CallFunction(Eggman_Function51)
			Object.XPos+=TempValue0
			CallFunction(Eggman_Function51)
		else
			Object.XPos+=TempValue0
			CallFunction(Eggman_Function51)
		endif
		TempValue0=Object.iXPos
		TempValue0-=16
		if Stage.XBoundary1<TempValue0
			Stage.XBoundary1=TempValue0
			if Stage.XBoundary1>15840
				Stage.XBoundary1=15840
			endif
		endif
		TempValue0=Object[-1].XPos
		TempValue0-=2228224
		if Object.XPos>TempValue0
			Object.XPos=TempValue0
			Object.Animation=0
			Object.Value1=0
			Object.State++
		endif
		break
	case 14
		if Player.Animation!=ANI_DYING
			if Object.Value0<50
				Object.Value0++
			else
				Object.Value0=0
				Object.Value2=0
				Object.State++
			endif
		endif
		break
	case 15
		if Object.Value0<20
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=1
			Object.State++
		endif
		break
	case 16
		if Object.Value0<20
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		break
	case 17
		if Object.Value0<20
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=1
			Object.State++
		endif
		break
	case 18
		if Object.Value0<20
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		break
	case 19
		if Object.Value0<60
			Object.Value0++
		else
			Object.Value0=0
			Object.State++
		endif
		break
	case 20
		Object.YPos+=131072
		if Object.Value0<42
			Object.Value0++
		else
			Object.Value0=0
			Object.Value2=0
			Object.State++
		endif
		break
	case 21
		if Object.Value0<60
			Object.Value0++
		else
			Object.Value0=0
			Object.Animation=1
			Object.State++
		endif
		break
	case 22
		Object.XPos+=393216
		if Object.OutOfBounds==1
			Object.Type=TypeName[BlankObject]
			Object.Priority=0
			Stage.NewXBoundary2=16352
		endif
		break

	endswitch
	if Object.Value2==1
		if Object.Value3==0
			CreateTempObject(TypeName[Explosion],0,Object.XPos,Object.YPos)
			Object[TempObjectPos].XPos+=Object.Value5
			Object[TempObjectPos].YPos+=11534336
			ArrayPos0=Object.Value4
			Object.Value4=Object[TempObjectPos].EntityNo
			Object.Value3=8
			if Object[ArrayPos0].DrawOrder==3
				Object[TempObjectPos].DrawOrder=4
				Rand(TempValue0,4)
				CreateTempObject(TypeName[Debris],TempValue0,Object.XPos,Object.YPos)
				Object[TempObjectPos].XPos+=Object.Value5
				Object[TempObjectPos].YPos+=11534336
				Rand(Object[TempObjectPos].Value1,6)
				Object[TempObjectPos].Value1<<=16
				Object[TempObjectPos].Value1-=163840
				Object[TempObjectPos].Value1+=Object.Value1
				Rand(Object[TempObjectPos].Value2,3)
				Object[TempObjectPos].Value2+=2
				Object[TempObjectPos].Value2<<=16
				FlipSign(Object[TempObjectPos].Value2)
			endif
		else
			ArrayPos0=Object.Value4
			Object[ArrayPos0].XPos=Object.XPos
			Object[ArrayPos0].XPos+=Object.Value5
			Object[ArrayPos0].YPos=Object.YPos
			Object[ArrayPos0].YPos+=11534336
			Object.Value3--
		endif
	else
		if Object.Value3>0
			ArrayPos0=Object.Value4
			Object[ArrayPos0].XPos=Object.XPos
			Object[ArrayPos0].XPos+=Object.Value5
			Object[ArrayPos0].YPos=Object.YPos
			Object[ArrayPos0].YPos+=11534336
			Object.Value3--
		endif
	endif
endsub


sub ObjectPlayerInteraction
	if Object.Value2==1
		if Object.State<12
			PlayerObjectCollision(C_TOUCH,-12,48,12,176)
			if CheckResult==1
				CallFunction(PlayerObject_Hit)
			endif
		else
			if Player.XPos<Object.XPos
				Player.XPos=Object.XPos
				if Player.RCollisionFlag==1
					CallFunction(PlayerObject_Kill)
				endif
			endif
			Player.RCollisionFlag=0
			if Object.State<14
				PlayerObjectCollision(C_TOUCH,-12,48,12,176)
			else
				PlayerObjectCollision(C_TOUCH,-64,-256,16,176)
			endif
			if CheckResult==1
				CallFunction(PlayerObject_Kill)
			endif
		endif
	endif
	if Player.YPos<Object.YPos
		if Player.XPos<234881024
			if Player.State==PlayerObject_HandleTailsFlight
				if Player.Timer<470
					Player.Timer=470
				endif
			endif
		endif
		if Player.XPos>948961280
			if Player.State==PlayerObject_HandleTailsFlight
				if Player.Timer<470
					Player.Timer=470
				endif
			endif
		endif
	endif
endsub


sub ObjectDraw
	switch Object.Animation
	case 0
		DrawSpriteFX(0,FX_FLIP,Object.XPos,Object.YPos)
		TempValue0=Object.AnimationTimer
		TempValue0/=6
		if TempValue0==1
			DrawSpriteFX(1,FX_FLIP,Object.XPos,Object.YPos)
			DrawSpriteFX(2,FX_FLIP,Object.XPos,Object.YPos)
			DrawSpriteFX(3,FX_FLIP,Object.XPos,Object.YPos)
		endif
		if Object.Value2==1
			TempValue0=Object.AnimationTimer
			TempValue0&=3
			TempValue0>>=1
			TempValue0+=12
			TempValue1=Object.XPos
			TempValue2=Object.YPos
			TempValue2+=3145728
			DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			if Object.Direction==FACING_RIGHT
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			else
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			endif
		endif
		Object.AnimationTimer++
		Object.AnimationTimer%=12
		break
	case 1
		TempValue0=Object.AnimationTimer
		TempValue0&=1
		if TempValue0==1
			Object.Frame++
			Object.Frame&=3
		endif
		TempValue0=Object.Frame
		TempValue0+=4
		DrawSpriteFX(TempValue0,FX_FLIP,Object.XPos,Object.YPos)
		TempValue0+=4
		DrawSpriteFX(TempValue0,FX_FLIP,Object.XPos,Object.YPos)
		TempValue0=Object.AnimationTimer
		TempValue0/=6
		DrawSpriteFX(0,FX_FLIP,Object.XPos,Object.YPos)
		if TempValue0==1
			DrawSpriteFX(1,FX_FLIP,Object.XPos,Object.YPos)
			DrawSpriteFX(2,FX_FLIP,Object.XPos,Object.YPos)
			DrawSpriteFX(3,FX_FLIP,Object.XPos,Object.YPos)
		endif
		if Object.Value2==1
			TempValue0=Object.AnimationTimer
			TempValue0&=3
			TempValue0>>=1
			TempValue0+=12
			TempValue1=Object.XPos
			TempValue2=Object.YPos
			TempValue2+=3145728
			DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			if Object.Direction==FACING_RIGHT
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1+=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			else
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
				TempValue2+=2097152
				TempValue1-=131072
				DrawSpriteXY(TempValue0,TempValue1,TempValue2)
			endif
		endif
		Object.AnimationTimer++
		Object.AnimationTimer%=12
		break

	endswitch
endsub


sub ObjectStartup
	LoadSpriteSheet("R7/Objects2.gif")
	SpriteFrame(-40,-24,72,72,91,1)
	SpriteFrame(-24,-9,24,16,164,1)
	SpriteFrame(22,27,8,8,75,99)
	SpriteFrame(-32,8,16,24,176,142)
	SpriteFrame(-68,-10,40,16,66,133)
	SpriteFrame(-60,-10,32,16,66,116)
	SpriteFrame(-68,-10,40,16,124,150)
	SpriteFrame(-60,-10,32,16,66,150)
	SpriteFrame(-63,30,32,16,66,150)
	SpriteFrame(-55,30,24,16,99,150)
	SpriteFrame(-63,30,32,16,66,116)
	SpriteFrame(-55,30,24,16,99,116)
	SpriteFrame(-8,0,16,32,91,74)
	SpriteFrame(-8,0,16,32,108,74)
endsub

sub RSDK
	LoadSpriteSheet("Global/Display.gif")
	SetEditorIcon(Icon0,SingleIcon,-16,-16,32,32,1,143)
endsub
